#include "Pnj.h"

/* ****************** */
/* ****Claase Pnj**** */
/* ****************** */

Pnj::Pnj(string n, string s, string f, string m)
    : moveAnim()
{
    name = n;
    surname = s;

    setImgPnjHead(f);
    setimgPnjMovement(m);
    isMoving = false;
    canMove = true;
    rotate(MOVE_UP);
    caseX = getPositionX()/32;
    caseY = getPositionY()/32;
}

Pnj::~Pnj()
{

}
void Pnj::setName(string n)
{
    name = n;
}
string& Pnj::getName()
{
    return name;
}
Sprite* Pnj::getPnjHead()
{
    return pnjHead;
}

void Pnj::setSurname(string s)
{
    surname = s;
}
string& Pnj::getSurname()
{
    return surname;
}

void Pnj::setImgPnjHead(string s)
{
    imgPnjHead = new Image;
    imgPnjHead->SetSmooth(false);
    linkPnjHead = s;
    imgPnjHead->LoadFromFile("Images/Personnages/" + s + "/face.png");
    pnjHead = new Sprite;
    pnjHead->SetImage(*imgPnjHead);


}
void Pnj::setimgPnjMovement(string s)
{
    imgPnjMovement = new Image;
    imgPnjMovement->SetSmooth(false);
    linkPnjMovement = s;
    imgPnjMovement->LoadFromFile("Images/Personnages/" + s + "/table.png");
    movement = new Sprite;
    movement->SetImage(*imgPnjMovement);
    int i = moveAnim.getDirection();
    movement->SetSubRect(IntRect( 0, i*SPRITE_HEIGHT, SPRITE_WIDTH, (i+1)*SPRITE_HEIGHT));
}
void Pnj::setPosition(int x, int y)
{
    movement->SetPosition(x,y);
    caseX = getPositionX()/32;
    caseY = getPositionY()/32;
}
int Pnj::getPositionX()
{
    return movement->GetPosition().x;
}
int Pnj::getPositionY()
{
    return movement->GetPosition().y;
}

void* Pnj::threadRunAnim(void* pnj)
{
    Pnj* perso = reinterpret_cast<Pnj*>(pnj);
    perso->runAnim();
}

bool Pnj::askAnim(int d, bool moveOn)
{
    bool m = false;
    if(!isMoving and canMove)
    {
        if(moveOn)
        {
            moveAnim.setDirection(d);
            isMoving = true;
            pthread_create(&thread, NULL, threadRunAnim, this);
            m = true;
        }

        else
        {
            rotate(d);
            m = false;
        }
    }
    return m;
}

void Pnj::runAnim()
{
    moveAnim.playMovement(*movement);
    isMoving = false;
    caseX = movement->GetPosition().x/32;
    caseY = movement->GetPosition().y/32;
}

Sprite* Pnj::getPnjMovement()
{
    return movement;
}

int Pnj::getCaseX()
{
    return caseX;
}

int Pnj::getCaseY()
{
    return caseY;
}

int Pnj::getDestCaseX()
{
    int d = moveAnim.getDirection();
    if(d==0 or d==2)
        return caseX;
    else if(d==1)
        return caseX-1;
    else
        return caseX+1;
}

int Pnj::getDestCaseY()
{
    int d = moveAnim.getDirection();
    if(d==1 or d==3)
        return caseY;
    else if(d==0)
        return caseY-1;
    else
        return caseY+1;
}

 void Pnj::rotate(int d)
 {
    movement->SetSubRect(IntRect( 0, d*SPRITE_HEIGHT, SPRITE_WIDTH, (d+1)*SPRITE_HEIGHT));
    moveAnim.setDirection(d);
 }

 int Pnj::getDirection()
 {
     return moveAnim.getDirection();
 }

bool Pnj::getIsMoving()
{
    return isMoving;
}

void Pnj::setIsMoving(bool b)
{
    isMoving = b;
}

void Pnj::setHeadPosition(int i, int j)
{
    pnjHead->SetPosition(i,j);
}

void Pnj::waitMovement()
{
    pthread_join (thread, NULL);
}
